package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.web;

import com.iwhalecloud.citybrain.flow.platform.manager.common.ErrorCode;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.WorkingDayRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.calendar.OffDayInfoVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.calendar.WorkingDayAndOffDayAddVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.calendar.WorkingDayInfoVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.calendar.WorkingDayQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wuliepeng
 * @date 2019-07-18
 */
@RestController
@RequestMapping("/calendar")
public class FlowWorkingDayController {

    @Autowired
    private WorkingDayRepository workingDayRepository;

    /**
     * 工作日设置新增或更新
     */
    @PostMapping("/addWorkingDayAndOffDay")
    public ResponseResult addWorkingDayAndOffDay(@RequestBody WorkingDayAndOffDayAddVo workingDayAndOffDayAddVo) {
        try {
            workingDayRepository.addWorkingDay(workingDayAndOffDayAddVo);
        } catch (Exception e) {
            return ResponseResult.fail(ErrorCode.BAD_REQUEST.errCode(), ErrorCode.BAD_REQUEST.errMessage());
        }
        return ResponseResult.success();
    }

    /**
     * 工作日设置列表查询
     */
    @PostMapping("/listWorkingDay")
    public ResponseResult listWorkingDay() {
        List<WorkingDayInfoVo> workingDayInfoVos = workingDayRepository.listWorkingDay();
        return ResponseResult.success(workingDayInfoVos).setTotalCount(workingDayInfoVos.size())
                .setPageSize(workingDayInfoVos.size());
    }

    /**
     * 节假日和例外设置列表查询
     */
    @PostMapping("/listOffDayAndException")
    public ResponseResult listOffDayAndException(@RequestBody WorkingDayQueryVo workingDayQueryVo) {
        List<OffDayInfoVo> offDayInfoVos;
        try {
            offDayInfoVos = workingDayRepository.listOffDayAndException(workingDayQueryVo);
        } catch (Exception e) {
            return ResponseResult.fail(ErrorCode.BAD_REQUEST.errCode(), ErrorCode.BAD_REQUEST.errMessage());
        }
        return ResponseResult.success(offDayInfoVos).setTotalCount(offDayInfoVos.size())
                .setPageSize(offDayInfoVos.size());
    }
}
